package com.ztf0337.mycurriculumdesign.ui.activity;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.Utils.TextDate;
import com.ztf0337.mycurriculumdesign.adapter.ParkLotAdapter;
import com.ztf0337.mycurriculumdesign.entity.ParkLot;
import com.ztf0337.mycurriculumdesign.entity.ParkingSpace;
import com.ztf0337.mycurriculumdesign.entity.User;
import com.ztf0337.mycurriculumdesign.service.ParkLotService;
import com.ztf0337.mycurriculumdesign.service.ParkingSpaceService;
import com.ztf0337.mycurriculumdesign.service.UserService;
import com.ztf0337.mycurriculumdesign.ui.fragment.MyParkingSpaceFragment;
import com.ztf0337.mycurriculumdesign.ui.fragment.OrderFragment;
import com.ztf0337.mycurriculumdesign.ui.fragment.ParkingSpaceFragment;
import com.ztf0337.mycurriculumdesign.ui.fragment.ProfitFragment;

import java.io.File;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private TextView  toolbarText;
    private UserService userService;
    private ParkLotService parkLotService;
    private ParkingSpaceService parkingSpaceService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences optionParkLot = this.getSharedPreferences("optionParkLot", Context.MODE_PRIVATE);
        SharedPreferences loginInfo = this.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        userService = new UserService(this);
        parkLotService = new ParkLotService(this);
        parkingSpaceService = new ParkingSpaceService(this);


        drawerLayout = findViewById(R.id.home_layout);
        LinearLayout searchLayout = findViewById(R.id.search);
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbarText = findViewById(R.id.toolbar_text);
        // 设置工具栏
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setTitle("停车位"); // 设置标题
        // 添加侧滑开关
        toolbarText.setText(optionParkLot.getString("parkOption","未选择停车场"));

        toolbarText.setOnClickListener(v -> {
            long parkLotId = optionParkLot.getLong("parkLotId", -1L);
            if (parkLotId == -1L) {
                Toast.makeText(this, "请选择停车场", Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent(this, ParkLotDetailActivity.class);
                intent.putExtra("id", parkLotId);
                this.startActivity(intent);
            }

        });

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        RecyclerView recyclerView = findViewById(R.id.rvLotRecycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        ParkLotAdapter adapter = new ParkLotAdapter(parkLotService.getAllParkLot());
        recyclerView.setAdapter(adapter);
        // 侧菜单项点击事件
        adapter.setOnItemClickListener(parkLot -> {
            optionParkLot.edit().putString("parkOption", parkLot.getParkLotName()).apply();
            optionParkLot.edit().putLong("parkLotId", parkLot.getId()).apply();
            Log.d("停车位","已选择并存入id "+parkLot.getId()+" 停车场 "+parkLot.getParkLotName());
            Log.d("停车位","选择的停车场状态"+parkLot.getStatus());
            toolbarText.setText(parkLot.getParkLotName());
            drawerLayout.closeDrawer(GravityCompat.START);
            replaceFragment(new ParkingSpaceFragment());
        });
        //底部菜单点击事件

        Intent intent = getIntent();
        if (intent != null) {
            int open = intent.getIntExtra("open", 1);
            switch (open) {
                case 1 :
                    replaceFragment(new ParkingSpaceFragment());
                    setToolbarVisibility(true);
                    searchLayout.setVisibility(VISIBLE);
                    bottomNavigationView.setSelectedItemId(R.id.nav_home);
                    break;
                case 2 :
                    replaceFragment(new MyParkingSpaceFragment());
                    setToolbarVisibility(false);
                    searchLayout.setVisibility(VISIBLE);
                    bottomNavigationView.setSelectedItemId(R.id.nav_my_parking_space);
                    break;
                case 3 :
                    replaceFragment(new OrderFragment());
                    setToolbarVisibility(false);
                    searchLayout.setVisibility(VISIBLE);
                    bottomNavigationView.setSelectedItemId(R.id.nav_my_order);
                    break;
                case 4 :
                    replaceFragment(new ProfitFragment());
                    setToolbarVisibility(false);
                    searchLayout.setVisibility(GONE);
                    bottomNavigationView.setSelectedItemId(R.id.nav_profile);
                    break;
                default :
                    Log.d("错误","位置返回打开界面值");
            }

        }

        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                replaceFragment(new ParkingSpaceFragment());
                setToolbarVisibility(true);
                searchLayout.setVisibility(VISIBLE);
                return true;
            } else if (itemId == R.id.nav_my_parking_space) {
                replaceFragment(new MyParkingSpaceFragment());
                setToolbarVisibility(false);

                searchLayout.setVisibility(VISIBLE);
                return true;
            } else if (itemId == R.id.nav_my_order) {
                replaceFragment(new OrderFragment());
                setToolbarVisibility(false);

                searchLayout.setVisibility(VISIBLE);
                return true;
            } else if (itemId == R.id.nav_profile) {
                replaceFragment(new ProfitFragment());
                setToolbarVisibility(false);

                searchLayout.setVisibility(GONE);
                return true;
            }
            return false;
        });
    }

    public void setToolbarVisibility(boolean visible) {
        Toolbar toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            if (visible) {
                toolbar.setVisibility(VISIBLE);
            } else {
                toolbar.setVisibility(GONE);
            }
        }
    }

    private void replaceFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.content_frame, fragment)
                .commit();
    }

}